package com.uvlab.sitespot.service.sedt;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/4/20.
 * modified by yanglu on 2016/4/27  update the methods
 */
public class JxSiteEditorService {

    protected String rootdir;
    public JxSiteEditorService() {
        return;
    }

    public void init(String rootdir) {
        if (rootdir == null)
            this.rootdir = "E:\\uvlab-site\\site-springboot";
        else
            this.rootdir = rootdir;
    }

    public List<JxNode> find() {
        return findinside(this.rootdir);
    }
    //找到路径为id的节点，返回id路径下所有的文件或文件夹
    public List<JxNode> find(String id) {
        // TODO
        this.rootdir=rootdir+id;
        return findinside(this.rootdir);
    }

    //将当前path路径下的文件或文件夹以list列表形式返回，list中存储Node对象
    protected List<JxNode> findinside(String path) {
        List<JxNode> list = new ArrayList<JxNode>();
        File cur = new File(path);
        String id,suffix, shortname, dataTimeStr,categories,content = null,parent;
        List<String> children;
        Date lastModified;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        File[] files = cur.listFiles();
        for (File f : files) {
            shortname = f.getName();
            //文件最后修改时间
            lastModified = new Date(f.lastModified());
            //把它转化为yyyy-MM-dd HH:mm:ss形式
            dataTimeStr = fmt.format(lastModified);
            suffix = shortname.substring(shortname.lastIndexOf(".") + 1);

//            String name = cur.getName() .substring(0, cur.getName().lastIndexOf("."));
            if (f.isDirectory() && (shortname.startsWith(".") || shortname.equals(suffix))) {
                suffix = "";
                categories="FOLDER";
                children=chil(f);
                content=null;
            }else{
                categories="FILE";
                children=null;
                content=getContent(f.getPath());
                //显示文件信息
 //               content=getContent(path);
            }
            parent=path;
            id=f.getPath();
            int state=1;
            list.add(new JxNode(id,id,shortname,categories,content,parent,children,dataTimeStr,suffix,null,state));
        }
        return list;
    }

    /**
     * Return the detail of the specified node.
     *
     * @param id
     * @return
     */
    public JxNode get(String id) {
        // TODO
        int i=0;
        String parent=null;
        String categories=null;
        parent=id.substring(0,id.lastIndexOf("\\"));

        File cur= new File(id);
        String content=null;
        if(isDir(cur)){
            categories="FOLDER";
        }else{
            categories="FILE";
        }
        List<String> children=chil(cur);
        String suffix = cur.getName().substring(cur.getName().lastIndexOf(".") + 1);
        Date lastModified = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        lastModified = new Date(cur.lastModified());
        String dataTimeStr = fmt.format(lastModified);
        String name = cur.getName();
        int state=2;
        JxNode node=new JxNode(cur.getPath(), cur.getPath(), cur.getName(),categories, content, parent, children, dataTimeStr, suffix, name,state);
        return (node);
    }
    //获取子节点相对路径列表 2016/04/27
    public List<String> chil(File f){
        List<String> list=new ArrayList<String>();
        File[] files = f.listFiles();
        for(File file : files){
            try {
                list.add(file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * Update the node.
     *
     * @param id
     * @param content
     * @param node
     * @return
     */
    public JxNode set(String id, JxNode node,String content) {
        // TODO
        node.setContent(content);
        return node;
    }

    /**
     * Save the data in the node parameter as a new file.
     *
     * @param node An structure containing information to be saved in a new file.
     * @return The JxNode object.
     */
    public JxNode add(JxNode node) {
        String id=node.getId();

        return node;
    }

    /**
     * Delete a node file from the file system. 2016/04/27
     *
     * @param id
     * @return
     */
    public boolean remove(String id) {
        // TODO
        File cur=new File(id);
        if(cur.exists()) {
            deleteFile(cur);
            return true;
        }else
            return false;
    }
    private void deleteFile(File file){
            if(file.isFile()){
                file.delete();
            }else if(file.isDirectory()){
                File files[] = file.listFiles();
                for(int i=0;i<files.length;i++){
                    this.deleteFile(files[i]);
                }
            }
            file.delete();
        }

    /**
     * 获得详细内容 2016/04/27
     */
    public String getContent(String path){
        InputStream inputStream=null;
    String content=null;
    try {
        inputStream=new FileInputStream(path);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int counte=0;
    byte[] b=new byte[1024];
    try {
        while((counte=inputStream.read(b))!=-1){
            if(b.length==counte) {
                byte[] b1=new byte[b.length*3/2+1];
                for(int i=0;i<b.length;i++)
                    b1[i]=b[i];
                b=b1;
            }
            content=new String(b,"UTF-8");
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}
    /**
     * 判断文件类型 2016/04/27
     */
    public boolean isDir(File file){
        if(file.isDirectory()){
            return true;
        }else
            return false;
    }
}



